package com.fr.ticket;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author 付荣
 * @version 创建时间：2019年4月9日 上午10:38:54
 */
public class GoodsDetail {
	
	private static final String uri = "https://sec-m.ctrip.com/restapi/soa2/12530/json/resourceAddInfoQOC?_fxpcqlniredt=09031036110935595145";
	private static final int tryTime = 3;
	public static Set<String> failList = new HashSet<>();
	final static String goodsIndex;
	
	static {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String s = sdf.format(new Date());
		goodsIndex = "goods_" + s;
	}
	public static void  handleGoodsDetail(long productId, JSONObject goodsObject, int timeAgin){
		GoodsDetailParam param = new GoodsDetailParam();
		param.setViewid(productId);
		long goodsId = goodsObject.getLongValue("resid");
		if (timeAgin > tryTime) {
			failList.add(productId + "-" + goodsId);
			return;
		}
		param.getResids().add(goodsId);
		CloseableHttpResponse response = null;
		int time = 1;
		try {
			HttpPost httpPost = new HttpPost(uri);
			StringEntity stringEntity = new StringEntity((param.toString()), "utf-8");
			httpPost.setEntity(stringEntity);
			httpPost.setHeader("Content-Type", "application/json");
			response = HttpClientUtil.doRequest(httpPost);
			while(response == null && time < tryTime){
				response = HttpClientUtil.doRequest(httpPost);
				time++;
			}
			if(response == null){
				System.out.println("获取商品详情失败,参数为：" + param);
				return;
			}
            String httpEntityContent = HttpClientUtil.getHttpEntityContent(response);
            if (StringUtils.isNotBlank(httpEntityContent)) {
            	JSONObject object = JSON.parseObject(httpEntityContent);
            	JSONObject data = (JSONObject)object.get("data");
            	//do something
            	List<JSONObject> list = (List<JSONObject>) data.get("ress");
            	if(CollectionUtils.isNotEmpty(list)){
            		list.get(0).putAll(goodsObject);
            		list.get(0).put("productId", productId);
            		try{
                		ESUtils.writeIndex(goodsIndex, goodsIndex, list);
                	}catch(Exception e){
                		System.err.println("写入商品索引报错" + e);
                	}
            	}
            }
		} catch (Exception e) {
			System.err.println("handleGoodsDetail" + JSON.toJSONString(e) + productId + "-" + goodsId);
		} finally {
			try {
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
